<?php 
/**
 * 商品控制器
 */
class GoodsController extends VerifyController{
	public $model;
	
	public function __init(){
		$this->model = K('Goods');
	}
	
	public function index(){
		$data = $this->model->all();
		$this->assign("data",$data);
	    $this->display();
	}
	public function edit(){
		$gid = Q('get.gid',0,'intval');
		if(IS_POST){
			if(!$this->model->edit($gid)){
				$this->error($this->model->error);
			}
			$this->success("修改成功");
		}
//		获取商品属性和属性类型传递前台 为商品属性和商品规格填写做准备
		$attr = M()->join("__typeattr__ ta join __specsattr__ sa on sa.typeattrid=ta.attrid")->where("gid={$gid} && attrtype='属性'")->getField('sa.attrval',true);
		$this->assign("attr",$attr);
		
		$spec = M()->join("__typeattr__ ta join __specsattr__ sa on sa.typeattrid=ta.attrid")->where("gid={$gid} && attrtype='规格'")->all();
		$this->assign("spec",$spec);
//		获取商品信息和商品详情传递前台
		$data = M()->join("__goods__ g join __commoditydetails__ c on g.gid=c.gid")->where("c.gid={$gid}")->find();
		$data['bigpic'] = explode(',', $data['bigpic']);
		$this->assign("data",$data);
//		获取分类和属性类型 为商品属性和商品规格填写做准备
		$attrData = M()->join("__category__ c join __typeattr__ t on c.typeid=t.tid")->where("cid={$data['cid']}")->all();
		foreach ($attrData as $k => $v) {
			$attrData[$k]["attrval"] = explode("|", $v['attrval']);
		}
		$this->assign("attrData",$attrData);
		
//		获取分类信息传递到前台  调用模型的getData获得组合好的数组
		$category = Data::tree(K("category")->all(), 'cname','cid','pid');
		$this->assign("category",$category);
//		调用品牌信息
		$brand = K("Brand")->all();
		$this->assign("brand",$brand);
//		显示页面
	    $this->display();
	}
	public function del(){
		$gid = Q('get.gid',0,'intval');
		if(!$this->model->where("gid={$gid}")->delete()){
			$this->error($this->model->error);
		}
		$commoditydetails = K("Commoditydetails");
		if(!$commoditydetails->where("gid={$gid}")->delete()){
			$this->error($commoditydetails->error);
		}
		$specsModel = K("Specs");
		if(!$specsModel->where("gid={$gid}")->delete()){
			$this->error($specsModel->error);
		}
		$this->success("删除成功");
	    $this->display();
	}
	public function add(){
		if(IS_POST){
			if(!$this->model->addData()){
				$this->error($this->model->error);
			}
			$this->success("添加商品成功");
		}
		//获取分类信息传递到前台
		//调用模型的getData获得组合好的数组
		$category = Data::tree(K("category")->all(), 'cname','cid','pid');
		$this->assign("category",$category);
		$brand = K("Brand")->all();
		$this->assign("brand",$brand);
		
		
	    $this->display();
	}
	/**
	 * uploadify上传
	 */
	public function upload(){
	     $upload = new Upload('Upload/Content/' . date('y/m'));
		 $file = $upload->upload();
		 if (empty($file)) {
		 	$this->ajax('上传失败');
		 } else {
		 	$data = $file[0];
			$this->ajax($data);
		 }
	}
	/**
	 * uploadify上传列表图
	 */
	public function uploadlist(){
	     $upload = new Upload('Upload/Content/' . date('y/m'));
		 $file = $upload->upload();
		 if (empty($file)) {
		 	$this->ajax('上传失败');
		 } else {
		 	//来这个地方可以缩略图处理
			
		 	$data = $file[0];
			$this->ajax($data);
		 }
	}
	
	/**
	 * 异步删除图片
	 */
	public function delImg(){
	    $path = Q('post.path');
		//删除图片
		unlink($path);
		
	}
	public function attrTypeAjax(){
		if(!IS_AJAX)$this->error("非法进入");
	    $cid = Q("post.cid",0,"intval");
		$data = M()->join("__category__ c join __typeattr__ t on c.typeid=t.tid where cid={$cid}")->all();
		foreach ($data as $k => $v) {
			$data[$k]["attrval"] = explode("|", $v['attrval']);
		}
		$this->ajax($data);
	}
	
}
